import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useOperationRecordStore = defineStore('operationRecord',() => {
    const operationRecords = ref([
        {
            memberId: 1,
            memberCardId: 1,
            operationTime: "2021-12-30 12:30:31",
            operationType: "绑卡操作",
            changeTimes: 0,
            changeMoney: -1,
            operator: '张老师',
            remark: '',
            status: '已激活'
        }
    ])

    const getOperationRecords = (memberId, memberCardId) => {
        return operationRecords.value.filter(item => item.memberId === memberId && item.memberCardId === memberCardId)
    }

    const addRecord = (record) => {
        record.operationTime = formatDateTime(new Date())
        operationRecords.value.push(record)
    }

    const formatDateTime = (date) => {
        var year = date.getFullYear();
        var month = (1 + date.getMonth()).toString().padStart(2, '0');
        var day = date.getDate().toString().padStart(2, '0');
        var hours = date.getHours().toString().padStart(2, '0');
        var minutes = date.getMinutes().toString().padStart(2, '0');
        var seconds = date.getSeconds().toString().padStart(2, '0');
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    }

    return{
        operationRecords,
        getOperationRecords,
        addRecord
    }
})
